﻿namespace HRM.CORE.Process
{
    using DevExpress.Utils;
    using DevExpress.XtraBars;
    using DevExpress.XtraEditors;
    using DevExpress.XtraGrid.Views.BandedGrid;
    using APP.HELPER;
    using Common.Class; using APP.HELPER.Utils.Language; using Security.Security; using Dictionary.ERP;
    using Common.Report;
    using HRM.CORE;
    using HRM.CORE.REPORT;
    using System; using Extra.ERP;
    using System.ComponentModel;
    using System.Windows.Forms; using Common;
    using Common;
    using Security.ERP;
    using Extra.ERP;

    public class xucDiscipline : Common.xucBaseBasic
    {
        private IContainer components;
        private System.DateTime m_BeginDate;
        private string m_EmployeeCode;
        private System.DateTime m_EndDate;
        private int m_IndexKey;

        public xucDiscipline()
        {
            this.components = null;
            this.m_EmployeeCode = "";
            this.m_IndexKey = 0;
            this.m_BeginDate = System.DateTime.Now;
            this.m_EndDate = System.DateTime.Now;
            this.InitializeComponent();
            base.ucToolBar.SetInterface();
        }

        public xucDiscipline(string EmployeeCode)
        {
            this.components = null;
            this.m_EmployeeCode = "";
            this.m_IndexKey = 0;
            this.m_BeginDate = System.DateTime.Now;
            this.m_EndDate = System.DateTime.Now;
            this.InitializeComponent();
            this.m_EmployeeCode = EmployeeCode;
            base.ucToolBar.SetInterface();
        }

        protected override void Add()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiProcessDiscipline") == "OK")
            {
                if (!MyRule.AllowAdd)
                {
                    MyRule.Notify();
                }
                else if (this.m_EmployeeCode == "")
                {
                    MessageBox.Show("Vui l\x00f2ng chọn nh\x00e2n vi\x00ean ở tr\x00ean!");
                }
                else
                {
                    xfmDisciplineAdd add = new xfmDisciplineAdd(Actions.Add, this.m_EmployeeCode);
                    add.Added += new xfmDisciplineAdd.AddedEventHander(this.frm_Added);
                    add.ShowDialog();
                }
            }
        }

        private void AddRow(HRM_PROCESS_DISCIPLINE Item)
        {
            AdvBandedGridView gbList = base.gbList;
            int focusedRowHandle = gbList.FocusedRowHandle;
            gbList.AddNewRow();
            focusedRowHandle = gbList.FocusedRowHandle;
            gbList.SetRowCellValue(focusedRowHandle, "DisciplineID", Item.DisciplineID);
            gbList.SetRowCellValue(focusedRowHandle, "Employeecode", Item.EmployeeCode);
            gbList.SetRowCellValue(focusedRowHandle, "DisciplineName", Item.DisciplineName);
            gbList.SetRowCellValue(focusedRowHandle, "Reason", Item.Reason);
            gbList.SetRowCellValue(focusedRowHandle, "Form", Item.Form);
            gbList.SetRowCellValue(focusedRowHandle, "Date", Item.Date);
            gbList.SetRowCellValue(focusedRowHandle, "DateOccurred", Item.DateOccurred);
            gbList.SetRowCellValue(focusedRowHandle, "Location", Item.Location);
            gbList.SetRowCellValue(focusedRowHandle, "Witnesses", Item.Witnesses);
            gbList.UpdateCurrentRow();
        }

        public override void Change()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiProcessDiscipline") == "OK")
            {
                if (!MyRule.AllowAccess)
                {
                    MyRule.Notify();
                }
                else
                {
                    HRM_PROCESS_DISCIPLINE item = new HRM_PROCESS_DISCIPLINE();
                    object cellValue = base.GetCellValue(base.m_RowClickEventArgs.RowIndex, "DisciplineID");
                    if (cellValue != null)
                    {
                        base.SetWaitDialogCaption("Đang kiểm tra dữ liệu....");
                        Guid disciplineID = new Guid(cellValue.ToString());
                        if (item.Get(disciplineID) != "OK")
                        {
                            this.DoHide();
                            XtraMessageBox.Show("Dữ liệu kh\x00f4ng tồn tại", "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                        else
                        {
                            this.DoHide();
                            xfmDisciplineAdd add = new xfmDisciplineAdd(Actions.Update, item);
                            add.Updated += new xfmDisciplineAdd.UpdatedEventHander(this.frm_Updated);
                            add.Added += new xfmDisciplineAdd.AddedEventHander(this.frm_Added);
                            add.ShowDialog();
                        }
                    }
                }
            }
        }

        public override void Delete()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiProcessDiscipline") == "OK")
            {
                if (!MyRule.AllowDelete)
                {
                    MyRule.Notify();
                }
                else if (!ClsOption.System2.IsQuestion || (XtraMessageBox.Show("Bạn c\x00f3 muốn x\x00f3a kh\x00f4ng?", "Th\x00f4ng b\x00e1o", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.No))
                {
                    object cellValue;
                    Guid guid;
                    base.SetWaitDialogCaption("Đang x\x00f3a...");
                    string str = "";
                    bool flag = false;
                    AdvBandedGridView gbList = base.gbList;
                    int[] selectedRows = gbList.GetSelectedRows();
                    HRM_PROCESS_DISCIPLINE hrm_process_discipline = new HRM_PROCESS_DISCIPLINE();
                    for (int i = selectedRows.Length; i > 0; i--)
                    {
                        flag = true;
                        cellValue = base.GetCellValue(selectedRows[i - 1], "DisciplineID");
                        if (cellValue != null)
                        {
                            SYS_LOG.Insert("Qu\x00e1 Tr\x00ecnh Kỷ Luật", "Xo\x00e1", cellValue.ToString());
                            guid = new Guid(cellValue.ToString());
                            str = hrm_process_discipline.Delete(guid);
                            if (str == "OK")
                            {
                                gbList.DeleteRow(selectedRows[i - 1]);
                            }
                            else if (str != "OK")
                            {
                                MessageBox.Show("Th\x00f4ng tin kh\x00f4ng được x\x00f3a\n" + str, "Th\x00f4ng b\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                            }
                        }
                    }
                    this.DoHide();
                    if (!flag && (gbList.DataSource != null))
                    {
                        RowClickEventArgs args = new RowClickEventArgs((gbList == null) ? -1 : gbList.FocusedRowHandle, (gbList.FocusedColumn == null) ? -1 : gbList.FocusedColumn.ColumnHandle, (gbList.FocusedColumn == null) ? "" : gbList.FocusedColumn.FieldName);
                        base.m_RowClickEventArgs = args;
                        cellValue = null;
                        cellValue = base.GetCellValue(args.RowIndex, "DisciplineCode");
                        if (cellValue != null)
                        {
                            SYS_LOG.Insert("Qu\x00e1 Tr\x00ecnh Kỷ Luật", "Xo\x00e1", cellValue.ToString());
                            base.SetWaitDialogCaption("Đang x\x00f3a...");
                            guid = new Guid(cellValue.ToString());
                            str = hrm_process_discipline.Delete(guid);
                            if (str == "OK")
                            {
                                gbList.DeleteRow(args.RowIndex);
                            }
                            else if (str != "OK")
                            {
                                MessageBox.Show("Th\x00f4ng tin kh\x00f4ng được x\x00f3a\n" + str, "Th\x00f4ng b\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                            }
                            this.DoHide();
                        }
                    }
                }
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        protected override bool ExportPermision()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiProcessDiscipline") != "OK")
            {
                return false;
            }
            if (!MyRule.AllowExport)
            {
                MyRule.Notify();
                return false;
            }
            SYS_LOG.Insert("Qu\x00e1 Tr\x00ecnh Kỷ Luật", "Xuất");
            return base.ExportPermision();
        }

        private void frm_Added(object sender, HRM_PROCESS_DISCIPLINE e)
        {
            this.AddRow(e);
        }

        private void frm_Updated(object sender, HRM_PROCESS_DISCIPLINE Item)
        {
            this.UpdateRow(Item, base.m_RowClickEventArgs);
        }

        protected override void gbList_MouseDown(object sender, MouseEventArgs e)
        {
            AdvBandedGridView view = (AdvBandedGridView) sender;
            RowClickEventArgs args = new RowClickEventArgs((view == null) ? -1 : view.FocusedRowHandle, (view.FocusedColumn == null) ? -1 : view.FocusedColumn.ColumnHandle, (view.FocusedColumn == null) ? "" : view.FocusedColumn.FieldName);
            base.m_RowClickEventArgs = args;
            object cellValue = base.GetCellValue(args.RowIndex, "DisciplineID");
            base.DisableMenu(false);
            if (cellValue == null)
            {
                base.DisableMenu(true);
            }
        }

        private void InitializeComponent()
        {
            this.components = new Container();
            base.AutoScaleMode = AutoScaleMode.Font;
        }

        protected override void List_Init(AdvBandedGridView dt)
        {
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                switch (dt.Columns[i].FieldName)
                {
                    case "DisciplineID":
                        dt.Columns[i].OptionsColumn.ReadOnly = true;
                        dt.Columns[i].OptionsColumn.AllowEdit = false;
                        dt.Columns[i].OptionsColumn.AllowGroup = DefaultBoolean.False;
                        dt.Columns[i].OptionsColumn.FixedWidth = true;
                        dt.Columns[i].Visible = false;
                        if (SysOption.Language == "VN")
                        {
                            dt.Columns[i].Caption = "M\x00e3";
                        }
                        dt.Columns[i].Width = 60;
                        break;

                    case "EmployeeCode":
                        dt.Columns[i].OptionsColumn.ReadOnly = true;
                        dt.Columns[i].OptionsColumn.AllowEdit = false;
                        dt.Columns[i].OptionsColumn.AllowGroup = DefaultBoolean.False;
                        dt.Columns[i].OptionsColumn.FixedWidth = true;
                        dt.Columns[i].Visible = false;
                        if (SysOption.Language == "VN")
                        {
                            dt.Columns[i].Caption = "M\x00e3 Nh\x00e2n Vi\x00ean";
                        }
                        dt.Columns[i].Width = 60;
                        break;

                    case "DisciplineName":
                        dt.Columns[i].OptionsColumn.ReadOnly = true;
                        dt.Columns[i].OptionsColumn.AllowEdit = false;
                        dt.Columns[i].OptionsColumn.AllowGroup = DefaultBoolean.False;
                        dt.Columns[i].OptionsColumn.FixedWidth = true;
                        if (SysOption.Language == "VN")
                        {
                            dt.Columns[i].Caption = "Kỷ luật về việc";
                        }
                        dt.Columns[i].Width = 180;
                        break;

                    case "DateOccurred":
                        dt.Columns[i].OptionsColumn.ReadOnly = true;
                        dt.Columns[i].OptionsColumn.AllowEdit = false;
                        dt.Columns[i].OptionsColumn.AllowGroup = DefaultBoolean.False;
                        dt.Columns[i].OptionsColumn.FixedWidth = true;
                        if (SysOption.Language == "VN")
                        {
                            dt.Columns[i].Caption = "Thời gian xảy ra";
                        }
                        dt.Columns[i].Width = 150;
                        break;

                    case "Location":
                        dt.Columns[i].OptionsColumn.ReadOnly = true;
                        dt.Columns[i].OptionsColumn.AllowEdit = false;
                        dt.Columns[i].OptionsColumn.AllowGroup = DefaultBoolean.False;
                        dt.Columns[i].OptionsColumn.FixedWidth = true;
                        if (SysOption.Language == "VN")
                        {
                            dt.Columns[i].Caption = "Địa điểm xảy ra";
                        }
                        dt.Columns[i].Width = 180;
                        break;

                    case "Witnesses":
                        dt.Columns[i].OptionsColumn.ReadOnly = true;
                        dt.Columns[i].OptionsColumn.AllowEdit = false;
                        dt.Columns[i].OptionsColumn.AllowGroup = DefaultBoolean.False;
                        dt.Columns[i].OptionsColumn.FixedWidth = true;
                        if (SysOption.Language == "VN")
                        {
                            dt.Columns[i].Caption = "Người chứng kiến";
                        }
                        dt.Columns[i].Width = 150;
                        break;

                    case "Form":
                        dt.Columns[i].OptionsColumn.ReadOnly = true;
                        dt.Columns[i].OptionsColumn.AllowEdit = false;
                        dt.Columns[i].OptionsColumn.AllowGroup = DefaultBoolean.False;
                        dt.Columns[i].OptionsColumn.FixedWidth = true;
                        if (SysOption.Language == "VN")
                        {
                            dt.Columns[i].Caption = "H\x00ecnh thức kỷ luật";
                        }
                        dt.Columns[i].Width = 150;
                        break;

                    case "Date":
                        dt.Columns[i].OptionsColumn.ReadOnly = true;
                        dt.Columns[i].OptionsColumn.AllowEdit = false;
                        dt.Columns[i].OptionsColumn.AllowGroup = DefaultBoolean.False;
                        dt.Columns[i].OptionsColumn.FixedWidth = true;
                        if (SysOption.Language == "VN")
                        {
                            dt.Columns[i].Caption = "Ng\x00e0y ra quyết định";
                        }
                        dt.Columns[i].Width = 150;
                        break;

                    default:
                        dt.Columns[i].Visible = false;
                        break;
                }
            }
        }

        protected override void Print()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiProcessDiscipline") == "OK")
            {
                if (!MyRule.AllowPrint)
                {
                    MyRule.Notify();
                }
                else
                {
                    rptListDisciplineByEmployee employee;
                    SYS_LOG.Insert("Qu\x00e1 Tr\x00ecnh Kỷ Luật", "In");
                    xfmReport report = new xfmReport("Qu\x00e1 Tr\x00ecnh Kỷ Luật");
                    if (this.m_IndexKey == 0)
                    {
                        employee = new rptListDisciplineByEmployee(this.m_EmployeeCode);
                    }
                    else
                    {
                        employee = new rptListDisciplineByEmployee(this.m_EmployeeCode, this.m_BeginDate, this.m_EndDate);
                    }
                    report.ShowPrintPreview(employee);
                }
            }
        }

        public override void ReLoad()
        {
            base.SetWaitDialogCaption("Đang nạp dữ liệu...");
            base.gcList.DataSource = new HRM_PROCESS_DISCIPLINE().GetListByEmployee(this.m_EmployeeCode);
            base.SetWaitDialogCaption("Đang nạp cấu h\x00ecnh...");
            this.List_Init(base.gbList);
            base.SetWaitDialogCaption("Nạp quyền sử dụng...");
            MyRule.Get(MyLogin.RoleId, "bbiProcessDiscipline");
            if (!MyRule.AllowPrint)
            {
                base.ucToolBar.bbiPrint.Visibility = BarItemVisibility.Never;
            }
            MyRule.Get(MyLogin.RoleId, "bbiProcessDiscipline");
            if (!MyRule.AllowExport)
            {
                base.ucToolBar.bbiExport.Visibility = BarItemVisibility.Never;
            }
            MyRule.Get(MyLogin.RoleId, "bbiProcessDiscipline");
            if (!MyRule.AllowAdd)
            {
                base.ucToolBar.bbiAdd.Visibility = BarItemVisibility.Never;
            }
            MyRule.Get(MyLogin.RoleId, "bbiProcessDiscipline");
            if (!MyRule.AllowDelete)
            {
                base.ucToolBar.bbiDelete.Visibility = BarItemVisibility.Never;
            }
            MyRule.Get(MyLogin.RoleId, "bbiProcessDiscipline");
            if (!MyRule.AllowEdit)
            {
                base.ucToolBar.bbiEdit.Visibility = BarItemVisibility.Never;
            }
            base.SetWaitDialogCaption("Đ\x00e3 xong...");
            this.DoHide();
        }

        public void SetData(string EmployeeCode)
        {
            this.m_IndexKey = 0;
            this.m_EmployeeCode = EmployeeCode;
            base.gcList.DataSource = new HRM_PROCESS_DISCIPLINE().GetListByEmployee(this.m_EmployeeCode);
        }

        public void SetData(string EmployeeCode, System.DateTime BeginDate, System.DateTime EndDate)
        {
            this.m_IndexKey = 1;
            this.m_EmployeeCode = EmployeeCode;
            this.m_BeginDate = BeginDate;
            this.m_EndDate = EndDate;
            base.gcList.DataSource = new HRM_PROCESS_DISCIPLINE().GetListByDays(this.m_EmployeeCode, BeginDate, EndDate);
        }

        private void UpdateRow(HRM_PROCESS_DISCIPLINE item, RowClickEventArgs e)
        {
            AdvBandedGridView gbList = base.gbList;
            int rowIndex = e.RowIndex;
            gbList.SetRowCellValue(rowIndex, "DisciplineID", item.DisciplineID);
            gbList.SetRowCellValue(rowIndex, "Employeecode", item.EmployeeCode);
            gbList.SetRowCellValue(rowIndex, "DisciplineName", item.DisciplineName);
            gbList.SetRowCellValue(rowIndex, "Reason", item.Reason);
            gbList.SetRowCellValue(rowIndex, "Form", item.Form);
            gbList.SetRowCellValue(rowIndex, "Date", item.Date);
            gbList.SetRowCellValue(rowIndex, "DateOccurred", item.DateOccurred);
            gbList.SetRowCellValue(rowIndex, "Location", item.Location);
            gbList.SetRowCellValue(rowIndex, "Witnesses", item.Witnesses);
            gbList.UpdateCurrentRow();
        }

        private void xucCustomerType_Load(object sender, EventArgs e)
        {
        }
    }
}

